package com.itheima.web;

import com.github.pagehelper.PageInfo;
import com.itheima.domain.Permission;
import com.itheima.domain.Role;
import com.itheima.domain.UserInfo;
import com.itheima.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import java.util.List;

@Controller
@RequestMapping(path = "/user")
public class UserController {

    @Autowired
    private UserService userService;

    /**
     * 查询所有用户
     * @param model
     * @param page
     * @param pageSize
     * @return
     */
    @RequestMapping("/findAll")
    public String findAll(Model model, @RequestParam(name = "page", required = true, defaultValue = "1") int page
            , @RequestParam(name = "pageSize", required = true, defaultValue = "4") int pageSize) {
        List<UserInfo> userInfoList = userService.findAll(page, pageSize);
        PageInfo pageInfo = new PageInfo(userInfoList);
        model.addAttribute("userList", pageInfo);
        return "user-list";
    }

    /**
     * 通过id查询用户
     *
     * @param model
     * @param id
     * @return
     */
    @RequestMapping("/findById")
    public String findById(Model model, String id) {
        UserInfo userInfoList = userService.findById(id);
        model.addAttribute("user", userInfoList);
        return "user-show";
    }

    /**
     * 添加用户
     *
     * @param model
     * @param userInfo
     * @return
     */
    @RequestMapping("/save")
    public String save(Model model, UserInfo userInfo) {
//        创建加密类
        BCryptPasswordEncoder passwordEncoder = new BCryptPasswordEncoder();
//        将密码进行加密
        userInfo.setPassword(passwordEncoder.encode(userInfo.getPassword()));
        int num = userService.save(userInfo);
        if (num != 0) {
            return "redirect:/user/findAll";
        }
        return "user-add";
    }

    /**
     * 通过用户id获取整个用户详情中还未添加的角色
     *
     * @param model
     * @param id
     * @return
     */
    @RequestMapping("/findUserByIdAndAllRole")
    public String findUserByIdAndAllRole(Model model, String id) {
        //获取用户详情
        UserInfo userInfo = userService.findById(id);
//        获得还允许添加的角色
        List<Role> roleList = userService.findUserNotRole(id);
        model.addAttribute("roleList", roleList);
        model.addAttribute("user", userInfo);
        return "user-role-add";
    }

    /**
     * 给用户添加角色
     * @param userId
     * @param ids
     * @return
     */
    @RequestMapping("/addRoleToUser")
    public String addRoleToUser(@RequestParam(name = "userId",required = true) String userId,@RequestParam(name = "ids" ,required = true) String[] ids) {

        int number = userService.addRoleToUser(userId,ids);
//        判断是否成功，用于返回对应的页面
        if (number != 0) {
            return "redirect:/user/findAll";
        }
               return "user-role-add";
    }
}
